/*
* @(#)ScanDmsDocumentAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.Crypt;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.LoginForm;
import com.dcivision.user.auth.AuthenticationHandler;
import com.dcivision.user.bean.UserRecord;

/**
  ScanDmsDocumentAction.java

  This class is for maint dms dmsDocument action

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   06/08/2003
    @version         $Revision: 1.17 $
    */

public class ScanDmsDocumentAction extends MaintDmsDocumentAction {

  public static final String REVISION = "$Revision: 1.17 $";
  
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_PUBLIC_FOLDER);
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    AuthenticationHandler authHandler = null;
    Connection conn = null;
    try {
      authHandler = (AuthenticationHandler)Class.forName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.AUTHENICATION_CLASS)).newInstance();
    } catch (Exception e) {
      log.error("Authenication Handler class not found.", e);
      return(mapping.findForward("Login"));
    }

    try {
      conn = (Connection)request.getAttribute(GlobalConstant.DB_KEY);
      SessionContainer sessionCtn = new SessionContainer();
      LoginForm loginForm = new LoginForm();
      ScanDmsDocumentForm scanForm = (ScanDmsDocumentForm)form;
      loginForm.setLoginName(scanForm.getLoginName());
      // Special logic to check whether the password passed in is encrypted or
      // not. If more than 20 characters, then assumed is encrypted.
      if (scanForm.getLoginPwd().length() > 20) {
        loginForm.setLoginPwd(Crypt.decrypt(scanForm.getLoginPwd(), SystemParameterFactory.getSystemParameter(SystemParameterConstant.CRYPTO_SALT)));
      } else {
        loginForm.setLoginPwd(scanForm.getLoginPwd());
      }
      loginForm.setUpdateFailedAttemptFlag(scanForm.getUpdateFailedAttemptFlag());
      log.debug("SCAN FORM - - LoginName = " + scanForm.getLoginName());
      UserRecord userRecord = authHandler.Login(loginForm, sessionCtn, request, conn);

      sessionCtn.setUserRecord(userRecord);
      sessionCtn.setLoginTime(Utility.getCurrentTimestamp());
      sessionCtn.setUserIPAddress(request.getRemoteAddr());
      sessionCtn.setLocale(Utility.getLocaleByString(GlobalConstant.LOCALE_STR_EN_US));
      sessionCtn.setLoginStatus(com.dcivision.framework.bean.AuditLoginOut.STATUS_LOGIN_SUCCESS);
      sessionCtn.setHttpSession(request.getSession());
      request.getSession().setAttribute(GlobalConstant.SESSION_CONTAINER_KEY, sessionCtn);
      synchronized (request.getSession()) {
        return (super.execute(mapping, form, request, response));
      }
    } catch (ApplicationException ae) {
      addError(request, "errors.login.login_failed");
      return(mapping.findForward("V"));
      //throw new ApplicationException("Login fail.");
    } catch (Exception e) {
      log.error("Error During Authenication.", e);
      return(mapping.findForward("LOGIN"));
    } finally {
      conn = null;
      try { request.getSession().invalidate(); } catch (Exception ignore) {}
    }
  }
}